/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin.DAO;

import Admin.Model.Banks;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class BankDAO {

    private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<Banks> GetAll() {
        ArrayList<Banks> listBank = new ArrayList<Banks>();
        Banks bank = null;
        try {

            strCommand = "select * from tbl_Banks";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
                bank = new Banks();
                bank.setBankID(rs.getInt("bankID"));
                bank.setBankName(rs.getString("bankName"));
                bank.setBankStatus(rs.getInt("bankStatus"));
                listBank.add(bank);

            }
            return listBank;
        } catch (SQLException ex) {
            Logger.getLogger(Banks.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return null;
    }

    public boolean AddBank(String bankName) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_Banks (bankName,bankStatus) VALUES (?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, bankName);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(BankDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public Banks GetByID(int bankID) {

        Banks bank = new Banks();
        try {

            strCommand = "select * from tbl_Banks WHERE bankID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, bankID);
            rs = ps.executeQuery();
            while (rs.next()) {
                bank.setBankID(rs.getInt("bankID"));
                bank.setBankName(rs.getString("bankName"));
                bank.setBankStatus(rs.getInt("bankStatus"));


            }
            return bank;
        } catch (SQLException ex) {
            Logger.getLogger(Banks.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return bank;
    }

    public boolean changeStatus(int bankID, int bankStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_Banks SET bankStatus = ? WHERE bankID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, bankStatus);
            ps.setInt(2, bankID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editBank(Banks objBank) {
        //UPDATE tbl_Banks SET bankName = ? WHERE bankID = ?
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_Banks SET bankName = ? WHERE bankID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objBank.getBankName());
            ps.setInt(2, objBank.getBankID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteBank(int bankID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_Banks WHERE bankID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, bankID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
